package edu.pku.sei.act.generatetemplate;

import java.util.ArrayList;
import edu.pku.sei.gmp.model.shape.GMPDiagram;
import edu.pku.sei.act.codegen.getinfor.ContentOfDiagram;
import edu.pku.sei.act.model.mote.Mote;
import edu.pku.sei.act.model.mote.Data;
import edu.pku.sei.act.model.mote.DataItem;
import edu.pku.sei.act.model.mote.LIP;
import edu.pku.sei.act.model.mote.Radio;
import edu.pku.sei.act.model.mote.Timer;
import edu.pku.sei.act.model.mote.SimpleDevice;
import edu.pku.sei.act.codegen.getinfor.ContentOfMote;
import edu.pku.sei.act.codegen.getinfor.MoteForCode;
import edu.pku.sei.act.codegen.getinfor.DataForCode;
import edu.pku.sei.act.codegen.getinfor.DataItemForCode;
import edu.pku.sei.act.codegen.getinfor.LIPForCode;
import edu.pku.sei.act.codegen.getinfor.RadioForCode;
import edu.pku.sei.act.codegen.getinfor.TimerForCode;
import edu.pku.sei.act.codegen.getinfor.SimpleDeviceForCode;

public class GenerateCode
{
  protected static String nl;
  public static synchronized GenerateCode create(String lineSeparator)
  {
    nl = lineSeparator;
    GenerateCode result = new GenerateCode();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "                 ";
 // protected final String TEXT_2 = NL + "   //printout data.";
  protected final String TEXT_3 = NL + "     ";
  protected final String TEXT_4 = " ";
  protected final String TEXT_5 = " final static ";
  protected final String TEXT_6 = " = ";
  protected final String TEXT_7 = ";";
  protected final String TEXT_8 = "                 ";
  protected final String TEXT_9 = NL + "     ";
  protected final String TEXT_10 = " ";
  protected final String TEXT_11 = " final ";
  protected final String TEXT_12 = " = ";
  protected final String TEXT_13 = "; ";
  protected final String TEXT_14 = NL + "     ";
  protected final String TEXT_15 = " ";
  protected final String TEXT_16 = " static ";
  protected final String TEXT_17 = " = ";
  protected final String TEXT_18 = ";";
  protected final String TEXT_19 = NL + "     ";
  protected final String TEXT_20 = " ";
  protected final String TEXT_21 = " ";
  protected final String TEXT_22 = " = ";
  protected final String TEXT_23 = "; ";
  protected final String TEXT_24 = NL + "     ";
  protected final String TEXT_25 = " ";
  protected final String TEXT_26 = " final static ";
  protected final String TEXT_27 = ";";
  protected final String TEXT_28 = NL + "     ";
  protected final String TEXT_29 = " ";
  protected final String TEXT_30 = " final ";
  protected final String TEXT_31 = "%>; ";
  protected final String TEXT_32 = NL + "     ";
  protected final String TEXT_33 = " ";
  protected final String TEXT_34 = " static ";
  protected final String TEXT_35 = ";";
  protected final String TEXT_36 = NL + "     ";
  protected final String TEXT_37 = " ";
  protected final String TEXT_38 = " ";
  protected final String TEXT_39 = "; ";
 // protected final String TEXT_40 = NL + "             " + NL + "             // radio concerned.";
  protected final String TEXT_41 = NL + "    static RadioTxDone onDone;" + NL + "    static byte[] msg = new byte[100];" + NL + "\tstatic byte[] xAddr = new byte[8];" + NL + "\tstatic long timeout;" + NL + "\tstatic Timer timer;" + NL + "" + NL + "\tstatic {" + NL + "\t\ttmp";
  protected final String TEXT_42 = " = new ";
  protected final String TEXT_43 = "();" + NL + "\t\tRadio.acquire();" + NL + "\t\tRadio.setPanId(0x01, false);" + NL + "" + NL + "\t\tRadio.getExtAddr(xAddr, 0);" + NL + "\t\tRadio.setShortAddr(Util.get16le(xAddr, 0));" + NL + "" + NL + "\t\tmsg[0] = Radio.FCF_DATA | Radio.FCF_NSPID;" + NL + "\t\tmsg[1] = Radio.FCA_SRC_SADDR | Radio.FCA_DST_SADDR;" + NL + "\t\tUtil.set16le(msg, 3, 0x01);" + NL + "\t\tUtil.set16le(msg, 7, Util.get16le(xAddr, 0));" + NL + "" + NL + "\t\tonDone = new RadioTxDone(null) {" + NL + "\t\t\tpublic void invoke(byte[] pdu, int len, int status, long txend) {" + NL + "\t\t\t\t";
  protected final String TEXT_44 = ".transmitDone(pdu, len, status, txend);" + NL + "\t\t\t}" + NL + "\t\t};" + NL + "" + NL + "\t\tRadio.setRxHandler((byte)0, new RadioRxPdu(null) {" + NL + "\t\t\tpublic void invoke(byte[] pdu, int len, long time, int quality) {" + NL + "\t\t\t\t";
  protected final String TEXT_45 = ".onRxPDU(pdu, len, time, quality);" + NL + "\t\t\t}" + NL + "\t\t});" + NL + "" + NL + "\t\tRadio.enableRx(Time.currentTicks() + 0x7FFFFFFF);" + NL + "" + NL + "\t\ttimer = new Timer();" + NL + "\t\ttimer.setCallback(new TimerEvent(null) {" + NL + "\t\t\tpublic void invoke(byte param, long time) {" + NL + "\t\t\t\t";
  protected final String TEXT_46 = ".periodicSend(param, time);" + NL + "\t\t\t}" + NL + "\t\t});" + NL + "\t\ttimeout = Time.toTickSpan(Time.MILLISECS, 2000);" + NL + "\t\ttimer.setAlarmBySpan(timeout);" + NL + "\t}" + NL + "\t" + NL + "\tpublic ";
  protected final String TEXT_47 = " (){" + NL + "\t}" + NL + "" + NL + "\tpublic static void transmitDone(byte[] pdu, int len, int status, long txend) {" + NL + "" + NL + "\t}" + NL + "" + NL + "\tpublic static void onRxTimeout (int info) {" + NL + "\t\tRadio.enableRx(Time.currentTicks() + 0x7FFFFFFF);" + NL + "\t}" + NL + "" + NL + "\tpublic static void periodicSend(byte param, long time) {" + NL + "\t\tboolean needTx = false;" + NL + "\t\tif (needTx) {" + NL + "\t\t\tRadio.transmit(Radio.TXMODE_CSMA, msg, 0, 100, onDone);" + NL + "\t\t}" + NL + "\t\ttimer.setAlarmBySpan(timeout);" + NL + "\t}" + NL + "" + NL + "\tpublic static void onRxPDU(byte[] pdu, int len, long time, int quality) {" + NL + "\t\tboolean needTx = false;" + NL + "\t\t////??????????????" + NL + "\t\tif (needTx) {" + NL + "\t\t\tRadio.transmit(Radio.TXMODE_CSMA, msg, 0, 100, onDone);" + NL + "\t\t}" + NL + "\t\t" + NL + "\t}";
  protected final String TEXT_48 = NL + " ";

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(TEXT_1);
     GMPDiagram diagram = (GMPDiagram) argument; 
     ContentOfDiagram contentOfDiagram = new ContentOfDiagram(diagram);
   MoteForCode moteForCode = contentOfDiagram.getMoteForCode();

     String name = moteForCode.getName();
   String status = moteForCode.getStatus();
   boolean radio = moteForCode.getRadio();
   boolean Lip = moteForCode.getLIP();
   boolean timer = moteForCode.getTimer();
   boolean simpleDevice = moteForCode.getSimpleDevice();
   DataForCode data = moteForCode.getData();
   ArrayList<DataItemForCode> datas = data.getData();
    //stringBuffer.append(TEXT_2);
     if(datas != null) { 
              int i;
              for(i=0;i<datas.size();i++) {
              DataItemForCode dt = datas.get(i);
               if(dt.getHasInitialValue()== true){
                 if(dt.getIsFinal() == true){
                   if(dt.getIsStatic() == true){ 
    stringBuffer.append(TEXT_3);
    stringBuffer.append(dt.getVisibility() );
    stringBuffer.append(TEXT_4);
    stringBuffer.append(dt.getType() );
    stringBuffer.append(TEXT_5);
    stringBuffer.append(dt.getVariable() );
    stringBuffer.append(TEXT_6);
    stringBuffer.append(dt.getVarValue() );
    stringBuffer.append(TEXT_7);
     }else { //isStatic
    stringBuffer.append(TEXT_8);
    stringBuffer.append(TEXT_9);
    stringBuffer.append(dt.getVisibility() );
    stringBuffer.append(TEXT_10);
    stringBuffer.append(dt.getType() );
    stringBuffer.append(TEXT_11);
    stringBuffer.append(dt.getVariable() );
    stringBuffer.append(TEXT_12);
    stringBuffer.append(dt.getVarValue() );
    stringBuffer.append(TEXT_13);
     } //isStatic
                 }else { //isFinal 
                    if(dt.getIsStatic() == true){ 
    stringBuffer.append(TEXT_14);
    stringBuffer.append(dt.getVisibility() );
    stringBuffer.append(TEXT_15);
    stringBuffer.append(dt.getType() );
    stringBuffer.append(TEXT_16);
    stringBuffer.append(dt.getVariable() );
    stringBuffer.append(TEXT_17);
    stringBuffer.append(dt.getVarValue() );
    stringBuffer.append(TEXT_18);
     }else { 
    stringBuffer.append(TEXT_19);
    stringBuffer.append(dt.getVisibility() );
    stringBuffer.append(TEXT_20);
    stringBuffer.append(dt.getType() );
    stringBuffer.append(TEXT_21);
    stringBuffer.append(dt.getVariable() );
    stringBuffer.append(TEXT_22);
    stringBuffer.append(dt.getVarValue() );
    stringBuffer.append(TEXT_23);
     } 
                 } 
     } else { //HasInitialValue
                 if(dt.getIsFinal() == true){
                   if(dt.getIsStatic() == true){ 
    stringBuffer.append(TEXT_24);
    stringBuffer.append(dt.getVisibility() );
    stringBuffer.append(TEXT_25);
    stringBuffer.append(dt.getType() );
    stringBuffer.append(TEXT_26);
    stringBuffer.append(dt.getVariable() );
    stringBuffer.append(TEXT_27);
     }else { 
    stringBuffer.append(TEXT_28);
    stringBuffer.append(dt.getVisibility() );
    stringBuffer.append(TEXT_29);
    stringBuffer.append(dt.getType() );
    stringBuffer.append(TEXT_30);
    stringBuffer.append(dt.getVariable() );
    stringBuffer.append(TEXT_31);
     } 
                } else { 
                   if(dt.getIsStatic() == true){ 
    stringBuffer.append(TEXT_32);
    stringBuffer.append(dt.getVisibility() );
    stringBuffer.append(TEXT_33);
    stringBuffer.append(dt.getType() );
    stringBuffer.append(TEXT_34);
    stringBuffer.append(dt.getVariable() );
    stringBuffer.append(TEXT_35);
     } else { 
    stringBuffer.append(TEXT_36);
    stringBuffer.append(dt.getVisibility() );
    stringBuffer.append(TEXT_37);
    stringBuffer.append(dt.getType() );
    stringBuffer.append(TEXT_38);
    stringBuffer.append(dt.getVariable() );
    stringBuffer.append(TEXT_39);
     } 
                 } 
     } //HasInitialValue
              } //for
             } //if 
    //stringBuffer.append(TEXT_40);
     if(radio==true){ 
    stringBuffer.append(TEXT_41);
    stringBuffer.append(name );
    stringBuffer.append(TEXT_42);
    stringBuffer.append(name );
    stringBuffer.append(TEXT_43);
    stringBuffer.append(name );
    stringBuffer.append(TEXT_44);
    stringBuffer.append(name );
    stringBuffer.append(TEXT_45);
    stringBuffer.append(name );
    stringBuffer.append(TEXT_46);
    stringBuffer.append(name );
    stringBuffer.append(TEXT_47);
     } 
     else {  
     } 
    stringBuffer.append(TEXT_48);
    return stringBuffer.toString();
  }
}
